Skip to content

feat(🎞️): createNativeBufferFromPointer takes a colorSpace - #642

Merged
wcandillon merged 13 commits into
wcandillon:mainfrom
andreialecu:feat/native-buffer-colorspace
Nov 25, 2026
Merged

wcandillon merged 13 commits into
wcandillon:mainfrom
andreialecu:feat/native-buffer-colorspace

Conversation

@andreialecu

@andreialecu andreialecu commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

I ran into this while building react-native-webcodecs, a WebCodecs implementation for React Native that works alongside react-native-webgpu. The two share native buffers: decoded video frames go to WebGPU through createNativeBufferFromPointer.

On Android, the AHardwareBuffers that MediaCodec hands out carry no color information. BT.709 video was read with the BT.601 matrix, and the colors came out visibly off.

As I understand it, the decoder knows the color space, since it's on the VideoFrame, but nothing passed it down to the YCbCr conversion. This PR lets the caller pass it.

On iOS, CVPixelBuffers already have their own attachments, so the option only overrides the fields you set.

Agent-written content

RNWebGPU.createNativeBufferFromPointer(pointer, { colorSpace }) takes a WebCodecs VideoColorSpaceInit (a VideoFrame's colorSpace fits as is) that says how to read a YUV buffer that carries no color information of its own, such as an AHardwareBuffer from a MediaCodec ImageReader. Without it, BT.709 video was read with the BT.601 matrix on Android ((226, 0, 31) for pure red). The fields it sets replace what the buffer says; a CVPixelBuffer keeps its own attachments otherwise.

Tests

NativeBufferColorSpace.spec.ts (7 cases) wraps the NV12 test frame's CVPixelBufferRef (BT.709 limited-range attachments) with createNativeBufferFromPointer and samples it through importExternalTexture:

  • no options and an empty colorSpace keep the buffer's attachments;
  • matrix bt709 / smpte170m, fullRange true / false, and matrix + fullRange each give the colors a reference YCbCr → sRGB conversion predicts, and the overrides visibly move the result.

JS has no CVPixelBufferRef to hand over (NativeBuffer.handle is the IOSurface), so a test-only RNWebGPU.createTestPixelBuffer, left out of the TypeScript declarations, returns the test frame with that pointer as its handle. The spec is skipped where the device has no IOSurface import: Android reads AHardwareBuffers through Vulkan's YCbCr sampler, which the expectations do not model.

Verified: the spec passes on the iOS 27.2 Simulator (8/8 together with ExternalTextureColorSpace). The spec was not run on Android.

On Android

A BT.709 limited-range video of 100% color bars, decoded by MediaCodec (frame.colorSpace is {"primaries":"bt709","transfer":"bt709","matrix":"bt709","fullRange":false}), with the frame imported through createNativeBufferFromPointer once without options and once with { colorSpace: frame.colorSpace.toJSON() }, then copied with copyExternalImageToTexture and read back (Android emulator, arm64):

Bar Without colorSpace With frame.colorSpace
red (225, 0, 31) (251, 1, 0)
green (0, 255, 49) (0, 254, 0)
blue (32, 2, 229) (0, 0, 252)
yellow (232, 251, 47) (252, 254, 0)
cyan (0, 255, 234) (0, 254, 252)
magenta (226, 0, 229) (251, 0, 251)

Reference bars, the frame without colorSpace (shifted), and with frame.colorSpace (matching)

`RNWebGPU.createNativeBufferFromPointer(pointer, { colorSpace })`: a WebCodecs
`VideoColorSpaceInit` (a `VideoFrame`'s `colorSpace` fits) that says how to
read a YUV buffer carrying no color information of its own, such as an
`AHardwareBuffer` from a MediaCodec `ImageReader`. Without it, BT.709 video
was read with the BT.601 matrix on Android ((226, 0, 31) for red). The fields
it sets replace what the buffer says; a `CVPixelBuffer` keeps its own
attachments otherwise.
…mPointer

Wraps the NV12 test frame's CVPixelBufferRef (BT.709 limited range
attachments) with createNativeBufferFromPointer and samples it through
importExternalTexture: without options and with an empty colorSpace the
attachments apply; matrix "bt709" / "smpte170m", fullRange true / false and
matrix + fullRange each give the colors a reference YCbCr -> sRGB
conversion predicts, and the overrides visibly move the result. Skipped
where the device has no IOSurface import (Android reads AHardwareBuffers
through Vulkan's YCbCr sampler, which the expectations do not model).

JS has no CVPixelBufferRef to hand over (NativeBuffer.handle is the
IOSurface), so a test-only RNWebGPU.createTestPixelBuffer, left out of the
TypeScript declarations, returns the test frame with that pointer as its
handle.
@andreialecu
andreialecu marked this pull request as ready for review November 24, 2026 19:14
@wcandillon
wcandillon self-requested a review November 25, 2026 09:19

@wcandillon wcandillon left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove createTestPixelBuffer and use the existing ColorSpace type, don't create a new one.

…Pointer

The colorSpace option is now the NativeBufferColorSpace that
importExternalTexture's sourceColorSpace takes, read by the same parser,
instead of a new WebCodecs-shaped type. createTestPixelBuffer and the e2e
spec that needed it are gone.
…sourceColorSpace

Wraps the AHardwareBuffer of the NV12 test frame with and without the
option and checks the result against importExternalTexture's
sourceColorSpace for the same members. Android only: on Apple platforms
no JS API gives a CVPixelBufferRef to wrap.
@andreialecu

andreialecu commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Done, thanks. createTestPixelBuffer is gone, and the colorSpace option of createNativeBufferFromPointer now takes NativeBufferColorSpace, parsed by the same code as sourceColorSpace in importExternalTexture.

@andreialecu
andreialecu marked this pull request as draft November 25, 2026 10:09
…pace

Wraps the test frame's AHardwareBuffer with its real color space (BT.709
narrow) and expects pure red and green, the same as sourceColorSpace gives.
One submit and one readback for the three draws, and no loose typing.
…ecks

The three imports are named (withoutColorSpace, withColorSpace,
withSourceColorSpace), the color space is the test frame's own, and a
color mismatch shows both pixels.
@andreialecu

Copy link
Copy Markdown
Contributor Author

I reworked the spec so it checks the actual bug. The test frame is encoded BT.709 narrow, but its AHardwareBuffer carries no color metadata, so wrapped without a colorSpace it gets the platform's guess. On the emulator that's BT.601, and pure red comes out as 228,0,32. Wrapped with { matrix: "rec709", range: "narrow" }, it comes out pure red and green, the same as sourceColorSpace gives. It fails without the change and passes with it.

@andreialecu
andreialecu marked this pull request as ready for review November 25, 2026 10:20
@wcandillon
wcandillon self-requested a review November 25, 2026 10:28
// how to read a YUV buffer that carries no color information of its own, as
// an AHardwareBuffer from a MediaCodec ImageReader. The members it sets
// replace what the buffer says.
jsi::Value createNativeBufferFromPointer(jsi::Runtime &runtime,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may I ask why it's back to the vanilla jsi::Value createNativeBufferFromPointer(jsi::Runtime &runtime, const jsi::Value &, ... signature? Sorry if I missed the obvious reason why?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No good reason, I copied the shape of createNativeBuffer next to it, which reads its options object by hand. I switched it back to a typed signature: the options are a small descriptor with its own JSIConverter, and its colorSpace is parsed by the same code as sourceColorSpace.

andreialecu and others added 3 commits November 25, 2026 12:37
The options are a CreateNativeBufferFromPointerOptions descriptor with its
own JSIConverter, whose colorSpace is parsed like sourceColorSpace, instead
of reading the raw jsi::Value arguments by hand.
@wcandillon
wcandillon self-requested a review November 25, 2026 11:24

@wcandillon wcandillon left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great sorry I still have two small requests (but otherwise, it's good to go for me):
apps/docs/content/api/native-buffer.mdx: update the documentation
CreateNativeBufferFromPointerOptions.h:

@@ -30,7 +30,7 @@ struct JSIConverter<std::shared_ptr<CreateNativeBufferFromPointerOptions>> {
       auto prop = arg.getObject(runtime).getProperty(runtime, "colorSpace");
       if (prop.isObject()) {
         result->colorSpace = external_texture_color_space::parse(
-            runtime, prop.getObject(runtime));
+            runtime, prop.getObject(runtime), "colorSpace");
       }
     }
     return result;

add a owner parameter to parse where owner can be sourceColorSpace or colorSpace.

@@ -51,7 +51,8 @@ namespace external_texture_color_space {
-inline wgpu::ColorSpaceYCbCrMatrixDawn parseMatrix(const std::string &name) {
+inline wgpu::ColorSpaceYCbCrMatrixDawn parseMatrix(const std::string &owner,
+                                                   const std::string &name) {
   if (name == "identity") {
@@ -64,12 +65,13 @@
-  throw std::runtime_error("sourceColorSpace.matrix: unknown value '" + name +
+  throw std::runtime_error(owner + ".matrix: unknown value '" + name +
                            "' (expected 'identity', 'rec601', 'rec709' or "
                            "'rec2020')");
 }
 
-inline wgpu::ColorSpaceYCbCrRangeDawn parseRange(const std::string &name) {
+inline wgpu::ColorSpaceYCbCrRangeDawn parseRange(const std::string &owner,
+                                                 const std::string &name) {
@@ -79,11 +81,12 @@
-  throw std::runtime_error("sourceColorSpace.range: unknown value '" + name +
+  throw std::runtime_error(owner + ".range: unknown value '" + name +
                            "' (expected 'identity', 'narrow' or 'full')");
 }
 
-inline wgpu::ColorSpacePrimariesDawn parsePrimaries(const std::string &name) {
+inline wgpu::ColorSpacePrimariesDawn parsePrimaries(const std::string &owner,
+                                                    const std::string &name) {
@@ -99,13 +102,13 @@
-  throw std::runtime_error("sourceColorSpace.primaries: unknown value '" +
-                           name +
+  throw std::runtime_error(owner + ".primaries: unknown value '" + name +
                            "' (expected 'srgb', 'rec709', 'rec601', "
                            "'rec2020' or 'display-p3')");
 }
 
-inline wgpu::ColorSpaceTransferDawn parseTransfer(const std::string &name) {
+inline wgpu::ColorSpaceTransferDawn parseTransfer(const std::string &owner,
+                                                  const std::string &name) {
@@ -127,13 +130,16 @@
-  throw std::runtime_error("sourceColorSpace.transfer: unknown value '" + name +
+  throw std::runtime_error(owner + ".transfer: unknown value '" + name +
                            "' (expected 'identity', 'srgb', 'display-p3', "
                            "'smpte-170m', 'hlg', 'pq' or 'bt-1886')");
 }
 
+// `owner` names the member being parsed in the error messages
+// ("sourceColorSpace", "colorSpace").
 inline VideoFrameColorSpace parse(jsi::Runtime &runtime,
-                                  const jsi::Object &value) {
+                                  const jsi::Object &value,
+                                  const std::string &owner) {
   VideoFrameColorSpace colorSpace;
@@ -146,16 +152,16 @@
   if (auto matrix = member("matrix")) {
-    colorSpace.matrix = parseMatrix(*matrix);
+    colorSpace.matrix = parseMatrix(owner, *matrix);
   }
   if (auto range = member("range")) {
-    colorSpace.range = parseRange(*range);
+    colorSpace.range = parseRange(owner, *range);
   }
   if (auto primaries = member("primaries")) {
-    colorSpace.primaries = parsePrimaries(*primaries);
+    colorSpace.primaries = parsePrimaries(owner, *primaries);
   }
   if (auto transfer = member("transfer")) {
-    colorSpace.transfer = parseTransfer(*transfer);
+    colorSpace.transfer = parseTransfer(owner, *transfer);
   }
   return colorSpace;
 }
@@ -204,7 +210,7 @@ struct JSIConverter<std::shared_ptr<rnwgpu::GPUExternalTextureDescriptor>> {
           result->sourceColorSpace = external_texture_color_space::parse(
-              runtime, prop.getObject(runtime));
+              runtime, prop.getObject(runtime), "sourceColorSpace");

…nd document the option

external_texture_color_space::parse takes the member it parses, so a bad
value passed to createNativeBufferFromPointer says colorSpace.matrix rather
than sourceColorSpace.matrix. The API docs show the options and when to
pass a colorSpace.
@andreialecu

Copy link
Copy Markdown
Contributor Author

@wcandillon no worries at all. Updated.

@wcandillon
wcandillon self-requested a review November 25, 2026 11:40
@wcandillon
wcandillon merged commit d2b1e76 into wcandillon:main Nov 25, 2026
9 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.26.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants